<?php
namespace Admin\Controller;
use Think\Controller;
class MessageController extends Controller{
	//显示所有评论
	public function index(){
		//接收检索数据
		$limits=array();
		$limits1='1';
		if(isset($_POST['is_varify'])) $limits['is_varify']=$_POST['is_varify']+0;
		if(isset($_POST['user_id'])) $limits1='m.user_id=0';
		//时间查询数据处理
		if (!empty($_POST['mes_createtime1'])) {
			$limits['mes_createtime']=empty($_POST['mes_createtime2'])?array('gt',strtotime($_POST['mes_createtime1'])):array(array('gt',strtotime($_POST['mes_createtime1'])),array('lt',strtotime($_POST['mes_createtime2'].'+1 day')));
		}else if (!empty($_POST['mes_createtime2'])) {
			$limits['mes_createtime']=array('lt',strtotime($_POST['mes_createtime2'].'+1 day'));
		}
		//dump($limits);
		//当前页数默认为第1页
		$p=isset($_GET['p'])?$_GET['p']:1;
		$pnum=5;
		//分页查询
		$usermodel=M('user');
		$messages=$usermodel
			->alias('u')
			->join('__MESSAGE__ m on u.user_id=m.user_id','right')
			->field('user_name,m.*')
			->where($limits)
			->where($limits1)
			->page($p,$pnum)
			->order('mes_id desc')
			->select();
		//时间数据处理
		$messages=$this->editTime($messages);
		//获取总条数
		$count=$usermodel
			->alias('u')
			->join('__MESSAGE__ m on u.user_id=m.user_id','right')
			->where($limits)
			->where($limits1)
			->count();
		//实例化分页对象
		$page=new \Think\Page($count,$pnum,$p);
		//获取分页信息
		$show=$page->show();
		//发送分页信息到模版
		$limits['num']=$count;
		$this->assign("limits",$limits);
		$this->assign("limits1",$limits1);
		$this->assign("pageinfo",$show);
		$this->assign("p",$p);
		//发送查询结果到页面
		$this->assign('messages',$messages);
		$this->display();	
	}


	//留言审核
	public function varify(){
		$data['mes_id']=$_GET['mes_id']+0;
		$data['is_varify']=1;
		if (M('message')->save($data)!==false) {
			$this->success('修改成功');
		}else{
			$this->error("修改失败!");
		}
	}


	/**
    * 将时间戳显示为时间
    * @param array $arr 待处理数组
    * @return array
    */
	private function editTime($arr){
		if(!isset($arr[0])){
			$arr['mes_createtime']=date("Y-m-d H:i:s", $arr['mes_createtime']);
		}else{
			foreach ($arr as $k => $v) {
				$arr[$k]=$this->editTime($v);
			}
		}
		return $arr;
	}
}